package com.tuyrt.hi.ui.banner.indicator;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.tuyrt.hi.library.util.HiDisplayUtil;
import com.tuyrt.hi.ui.R;

/**
 * author: Administrator on 2021/5/6.
 * desc: 圆形指示器
 */
public class HiCircleIndicator_java extends FrameLayout implements HiIndicator<FrameLayout> {

    private static final int VWC = ViewGroup.LayoutParams.WRAP_CONTENT;

    /**
     * 正常状态下的指示点
     */
    private @DrawableRes
    int mPointNormal = R.drawable.shape_point_normal;
    private @DrawableRes
    int mPointSelected = R.drawable.shape_point_select;

    /**
     *  指示点左右的间距
     */
    private int mPointLeftRightPadding = HiDisplayUtil.dp2px(5f, getResources());
    /**
     *  指示点上下的间距
     */
    private int mPointTopBottomPadding = HiDisplayUtil.dp2px(15f, getResources());

    public HiCircleIndicator_java(@NonNull Context context) {
        super(context);
    }

    public HiCircleIndicator_java(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public HiCircleIndicator_java(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public FrameLayout get() {
        return this;
    }

    @Override
    public void onInflate(int count) {
        removeAllViews();
        if (count <= 0) {
            return;
        }

        LinearLayout groupView = new LinearLayout(getContext());
        LinearLayout.LayoutParams imageViewParams = new LinearLayout.LayoutParams(VWC, VWC);
        imageViewParams.gravity = Gravity.CLIP_HORIZONTAL;
        imageViewParams.setMargins(mPointLeftRightPadding, mPointTopBottomPadding, mPointLeftRightPadding, mPointTopBottomPadding);
        ImageView pointView;
        for (int i = 0; i < count; i++) {
            pointView = new ImageView(getContext());
            pointView.setLayoutParams(imageViewParams);
            if (i == 0) {
                pointView.setImageResource(mPointSelected);
            } else {
                pointView.setImageResource(mPointNormal);
            }
            groupView.addView(pointView);
        }
        LayoutParams groupViewParams = new LayoutParams(VWC, VWC);
        groupViewParams.gravity = Gravity.CENTER | Gravity.BOTTOM;
        addView(groupView, groupViewParams);
    }

    @Override
    public void onPointChange(int current, int count) {
        ViewGroup groupView = (ViewGroup) getChildAt(0);
        for (int i = 0; i < groupView.getChildCount(); i++) {
            ImageView pointView = (ImageView) groupView.getChildAt(i);
            if (i == current) {
                pointView.setImageResource(mPointSelected);
            } else {
                pointView.setImageResource(mPointNormal);
            }
            pointView.requestLayout();
        }
    }
}
